﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com> :
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// ExplorerListViewHeader.cs:
//
// Cabeçalho da ListView no mode 'Details'
//
//-------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WBFSSync.AeroControls
{
    public partial class ExplorerListViewHeader : UserControl
    {
        //----------------------- Variáveis

        ExplorerListView associatedlistview = null;
        
        Point mousePosition = new Point(-1, -1);
        bool leftDown = false;
        int column_under_mouse = -1;

        bool resize = true;
        int resizedColumnPos = 0;

        int sortingsource = 0;
        bool sortup = true;

        bool canSort = true;
        public bool CanSort { get { return canSort; } set { canSort = value; } }

        SolidBrush HeaderTextColor = new SolidBrush(Color.FromArgb(76, 96, 122));

        StringFormat format = new StringFormat();

        //----------------------- Rotinas

        public ExplorerListViewHeader()
        {
            DoubleBuffered = true;
            InitializeComponent();
            format.Trimming = StringTrimming.EllipsisCharacter;
            format.Alignment = StringAlignment.Near;
            format.LineAlignment = StringAlignment.Center;
        }

        public bool CheckMouseIntersection(int x, int y, int h, int w)
        {
            return ((mousePosition.X >= x) && (mousePosition.X < x + w) &&
                    (mousePosition.Y >= y) && (mousePosition.Y < y + h));
        }

        public void AssociateWithListView(ExplorerListView listview)
        {
            associatedlistview = listview;
            listview.ViewModeChanged += new EventHandler(listview_ViewModeChanged);

            if (associatedlistview.ViewMode == ViewMode.Details)
            { 
                Visible = true;

                if (canSort)
                {
                    if (sortup) associatedlistview.Sorting = SortOrder.Ascending;
                    else associatedlistview.Sorting = SortOrder.Descending;
                }
            } 
            else 
            { 
                Visible = false;
                if (canSort) associatedlistview.Sorting = SortOrder.Ascending;
            }

        }

        void listview_ViewModeChanged(object sender, EventArgs e)
        {
            if (associatedlistview.ViewMode == ViewMode.Details) { Visible = true; } else { Visible = false; }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            mousePosition = e.Location;
            base.OnMouseMove(e);

            if (leftDown && resize)
            {
                associatedlistview.Columns[column_under_mouse].Width = mousePosition.X - resizedColumnPos;
                Invalidate();
            }
            else
            {

                int pos = 0;
                int width = 0;

                for (int i = 0; i < associatedlistview.Columns.Count; i++)
                {
                    width = associatedlistview.Columns[i].Width;

                    if (CheckMouseIntersection(pos + width - 3, 0, 24, 3)) //Splitter
                    {
                        Cursor = Cursors.VSplit;
                        resize = true;
                        column_under_mouse = i;
                        resizedColumnPos = pos;
                        Invalidate();
                        return;
                    }
                    else if (CheckMouseIntersection(pos, 0, 24, width - 3))
                    {
                        Cursor = Cursors.Default;
                        resize = false;

                        if (column_under_mouse != i)
                        {
                            column_under_mouse = i;
                            Invalidate();
                        }

                        return;
                    }

                    pos += width;
                }

                Cursor = Cursors.Default;
                resize = false;
                column_under_mouse = -1;
                Invalidate();
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            column_under_mouse = -1;
            leftDown = false;
            Invalidate();
            base.OnMouseLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) leftDown = true;
            if (column_under_mouse >= 0) Invalidate();
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) leftDown = false;
            if (column_under_mouse >= 0) Invalidate();
            base.OnMouseUp(e);
        }

        protected override void OnClick(EventArgs e)
        {
            if (!leftDown) return;

            if (resize) return;

            if (column_under_mouse >= 0)
            {
                if (column_under_mouse != sortingsource)
                {
                    sortingsource = column_under_mouse;
                }
                else
                    sortup = !sortup;

                if (canSort)
                {
                    if (sortup) associatedlistview.Sorting = SortOrder.Ascending;
                    else associatedlistview.Sorting = SortOrder.Descending;

                    associatedlistview.SortingColumn = column_under_mouse;
                }

                Invalidate();
            }

            base.OnClick(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
            e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            if (associatedlistview == null) return;

            int pos = 0;
            int width = 0;

            for (int i = 0; i < associatedlistview.Columns.Count; i++)
            {
                width = associatedlistview.Columns[i].Width;
                Rectangle bounds = new Rectangle(pos, 0, width, Height);

                DrawHeader(e.Graphics, bounds, associatedlistview.Columns[i].Text, column_under_mouse == i, i);

                pos += width;
            }

            base.OnPaint(e);
        }

        private void DrawHeader(Graphics Graphics, Rectangle Bounds, String text, bool mouseover, int index)
        {
            if (leftDown && mouseover)
            {
                Graphics.DrawImage(Skin.explorer_header_l_c, Bounds.X, Bounds.Y, 2, 24);
                Graphics.DrawImage(Skin.explorer_header_m_c, Bounds.X + 2, Bounds.Y, Bounds.Width - 4, 24);
                Graphics.DrawImage(Skin.explorer_header_r_c, Bounds.Right - 2, Bounds.Y, 2, 24);
            }
            else
            {
                if (mouseover)
                {
                    Graphics.DrawImage(Skin.explorer_header_l_o, Bounds.X, Bounds.Y, 2, 24);
                    Graphics.DrawImage(Skin.explorer_header_m_o, Bounds.X + 2, Bounds.Y, Bounds.Width - 4, 24);
                    Graphics.DrawImage(Skin.explorer_header_r_o, Bounds.Right - 2, Bounds.Y, 2, 24);
                }
                else
                {
                    Graphics.DrawImage(Skin.explorer_header_l, Bounds.X, Bounds.Y, 2, 24);
                    Graphics.DrawImage(Skin.explorer_header_m, Bounds.X + 2, Bounds.Y, Bounds.Width - 4, 24);
                    Graphics.DrawImage(Skin.explorer_header_r, Bounds.Right - 2, Bounds.Y, 2, 24);
                }
            }

            if (canSort)
            {
                if (index == sortingsource)
                {
                    if (sortup) Graphics.DrawImage(Skin.explorer_header_arrow_up, Bounds.Left + Bounds.Width / 2, Bounds.Y + 1, 7, 4);
                    else Graphics.DrawImage(Skin.explorer_header_arrow_down, Bounds.Left + Bounds.Width / 2, Bounds.Y + 1, 7, 4);
                }
            }

            Graphics.DrawString(text, Skin.CaptionFont, HeaderTextColor, new RectangleF(Bounds.X + 4,
                Bounds.Y + 5, Bounds.Width - 4, Skin.CaptionFont.Height), format);
        }
    }
}
